<template>
	<view>
		<view class="navbar">
			<u-navbar bgColor="#ffffff" safeAreaInsetTop :placeholder="true" titleStyle="{fontSize: 16px;font-weight: bold;color: #000000;}">
				<view class="navbar-left" slot="left" @click="cancel">
					<u-icon name="arrow-left"></u-icon>
				</view>
				<view class="navbar-center" slot="center">
					轻松小镇
				</view>
				<view class="navbar-right flex-no-horizontal" slot="right" @click="onAPPShare()">
					分享
				</view>
			</u-navbar>
		</view>
		<web-view :src="webViewUrl"  @message="getMessage" @postMessage="getPostMessage($event)"></web-view>
	</view>
</template>

<script>
	import {
		shareFriend,
		shareMoment,
		saveAlbum,
		appShare
	} from '../../utility/share.js'
	export default {
		data() {
			return {
				webViewUrl: '',
				title:''
			}
		},
		methods: {
			onAPPShare(){
				appShare('http://activity.relaverse.cn/#/yqMallIndex','轻松小镇','元气当铺正式上线')
			},
			getPostMessage(data){
				console.log('接收到消息1111~~', data)
			},
			getMessage(data) {
				console.log('接收到消息~~', data.detail.data[0])
				const {action, invitationCode} = data.detail.data[0]
				if (action) {
					//未实名先去实名
					// if(action == 'realName'){
					// 	uni.navigateTo({
					// 		url:'/pages/user/realName'
					// 	})
					// 	return false
					// }
					//忘记密码去进行修改
					if(action == 'realPassword'){
						uni.navigateTo({
							url:'/pages/safe/index'
						})
						return false
					}
					if(action == 'realAddress'){
						if(data.detail.data[0].hasOwnProperty('id')){
							uni.navigateTo({
								url:'/pages/user/setAddress?id=' + data.detail.data[0].id
							})
							return false
						}
						uni.navigateTo({
							url:'/pages/user/accountAddress'
						})
						return false
					}
					uni.showActionSheet({
						itemList: ['分享给朋友', '分享到朋友圈', '保存到相册'],
						success: function(res) {
							console.log(res)
							switch (res.tapIndex) {
								case 0:
									shareFriend(action)
									break;
								case 1:
									shareMoment(action)
									break;
								case 2:
									saveAlbum(action)
									break;
								default:
									break;
							}
						},
					})
				} else {
					uni.showToast({
						title: '暂未获得图片地址',
						icon: 'none'
					})
				}
			}
		},
		onLoad(e) {
			let url;
			if(e.token){
				url = `${e.url}&token${e.token}`
			} else {
				url = `${e.url}`
			}
			this.webViewUrl = url
			this.title = e.title || ''
			setTimeout(() => {
				uni.setNavigationBarTitle({
					title: this.title
				});
			},500)
		}
	}
</script>

<style lang="scss" scoped>
	.navbar {
		.navbar-left {
			color: $uni-color-black;
			font-size: $uni-font-size-lg;
		}
		.navbar-center {
			color: $uni-color-black;
			font-size: $uni-font-size-lg;
			font-weight: bold;
		}
		.navbar-right {
			font-size: $uni-font-size-base;
		}
	}
</style>
